<!doctype html>
<html>
  <head>
    <title>Notifications: Requesting permission using the Promise return value.</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../resources/permissions-helper.js"></script>
  </head>
  <body>
    <script>
      // Tests that Notification.requestPermission() returns a promise that will be
      // resolved with the current permission level. This test cannot be ran manually
      // because of the permission shifting going on.
      promise_test(test => {
        // Default value in layout tests is "denied".
        return Notification.requestPermission().then(permission => {
          assert_equals(permission, 'denied');

          return PermissionsHelper.setPermission('notifications', 'granted');
        }).then(function() {
          return Notification.requestPermission();
        }).then(permission => {
          assert_equals(permission, 'granted');
        });

      }, 'Requesting permission returns a promise that will be resolved.');

      // Tests that Notification.requestPermission() still invokes the callback (before
      // resolving the promise) and that the values are equal. Like the previous test,
      // this cannot be ran manually.
      promise_test(test => {
        var callbackPermission = 'undefined';

        function permissionCallback(value) {
          callbackPermission = value;
        }

        return PermissionsHelper.setPermission('notifications', 'denied').then(function() {
          return Notification.requestPermission(permissionCallback).then(permission => {
            assert_equals(permission, 'denied');
            assert_equals(permission, callbackPermission);

            return PermissionsHelper.setPermission('notifications', 'granted');
          }).then(function() {
            return Notification.requestPermission(permissionCallback);
          }).then(permission => {
            assert_equals(permission, 'granted');
            assert_equals(permission, callbackPermission);
          });
        })
      }, 'Requesting permission returns a promise, maintains the callback behaviour.');
    </script>
  </body>
</html>
